package gz.metro.datasync.utils.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

/**
 * @author ：张任豪
 * @date ：2022/9/30 11:14
 * @description：
 */
public class PagerVO<T> implements Serializable {

    private final static Integer DEFAULT_PAGE_SIZE = 10;
    @ApiModelProperty(notes = "每页显示数量", required = true)
    private Integer pageSize;
    @ApiModelProperty(notes = "总页数", required = true)
    private Long total;
    @ApiModelProperty(notes = "当前页", required = true)
    private Integer page;
    @ApiModelProperty(notes = "总记录数", required = true)
    private Long records;
    @ApiModelProperty(notes = "是否有下一页", required = true)
    private Boolean hasNext;
    @ApiModelProperty(notes = "是否有上一页", required = true)
    private Boolean hasPre;
    @ApiModelProperty(notes = "数据列表", required = true)
    private List<T> rows;

    /**
     * @param page     当前页数
     * @param pageSize 每页显示多少
     * @param total    总记录数
     */
    public PagerVO(Integer page, Integer pageSize, Integer total) {
        this(page, pageSize, total.longValue());
    }

    public PagerVO(Integer page, Integer pageSize, Long total, List<T> rows) {
        this(page, pageSize, total);
        this.rows = rows;
    }

    public static PagerVO of(Integer page, Integer pageSize, Long total, List rows) {
        return new PagerVO(page, pageSize, total, rows);
    }

    /**
     * pagehelper中的 PageInfo 转 PagerVO
     *
     * @param pageInfo
     * @return
     */
    public static PagerVO of(PageInfo pageInfo) {
        return new PagerVO(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * pagehelper中的 Page 转 PagerVO
     *
     * @param page
     * @return
     */
    public static PagerVO of(Page page) {
        return new PagerVO(page.getPageNum(), page.getPageSize(), page.getTotal(), page.getResult());
    }


    public static PagerVO of(IPage iPage) {
        return new PagerVO(Long.valueOf(iPage.getCurrent()).intValue(), Long.valueOf(iPage.getSize()).intValue(), iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 分页构造器
     *
     * @param pageSize 每页显示多少
     * @param page     当前页数
     * @param total    总记录数
     */
    public PagerVO(Integer page, Integer pageSize, Long total) {
        if (pageSize < 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }

        this.pageSize = pageSize;
        this.records = total;
        this.total = (total + pageSize - 1) / pageSize;

        if (page <= 0) {
            page = 1;
        }
        if (this.total == 0) {
            page = 1;
        } else {
            if (page != 1 && page > this.total) {
                page = this.total.intValue();
            }
        }
        //是否有上一页
        if (page <= 1) {
            this.hasPre = false;
        } else {
            this.hasPre = true;
        }
        //是否有下一页
        if (page.longValue() >= this.total) {
            this.hasNext = false;
        } else {
            this.hasNext = true;
        }

        this.page = Integer.valueOf(page);
    }


    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Long getrecords() {
        return records;
    }

    public void setrecords(Long records) {
        this.records = records;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Boolean getHasNext() {
        return hasNext;
    }

    public void setHasNext(Boolean hasNext) {
        this.hasNext = hasNext;
    }

    public Boolean getHasPre() {
        return hasPre;
    }

    public void setHasPre(Boolean hasPre) {
        this.hasPre = hasPre;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }
}